package cn.pubinfo.commentapi.controller;

import cn.pubinfo.commentapi.model.Tour;
import cn.pubinfo.commentapi.service.TourService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author kuancz
 * @date 2018/7/24
 */
@RestController
public class TourController {
    @Resource
    private TourService tourService;

    @GetMapping("{area}/{category}s")
    public ResponseEntity<List<Tour>> list(@PathVariable("category") String category, @PathVariable("area") String area){
        List<Tour> tourList = tourService.list(area, category);
        if (tourList.size() == 0){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(tourList, HttpStatus.OK);
    }

    @GetMapping("{area}/{category}s/{id:\\d+}")
    public ResponseEntity<Tour> get(@PathVariable("area") String area, @PathVariable("category") String category, @PathVariable("id") Integer id){
        Tour tour = tourService.get(area, category, id);
        if (tour == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(tour, HttpStatus.OK);
    }

    @GetMapping("{area}/{category}s/count")
    public ResponseEntity<Long> count(@PathVariable("area") String area, @PathVariable("category") String category){
        Long count = tourService.count(area, category);
        return new ResponseEntity<>(count, HttpStatus.OK);
    }
}
